package main

import (
	"fmt"
	"math"
	"io/ioutil"
	"net/http"
	"net/url"
)
func GetData() {
	client := &http.Client{}
	resp, err := client.Get("http://api.map.baidu.com/place/v2/suggestion?query=广州市天河区正佳广场&region=广州&city_limit=true&output=json&ak=yX8nC9Qzpckek7lY9gGWmlD4TFcA2tzYx3")
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(body))
}

func PostData() {
	postValue := url.Values{
		"apiName": {"gf"},
		"doSomeThing": {"\"name\":\"thounder\",\"age\":11,\"sex\":1\""},
		"time": {"123"},
	}
	response, err := http.PostForm("http://mock-api.com/Dmgv9AzQ.mock/gsonformat",postValue)
	if err!= nil {
		fmt.Println(err)
	}
	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)
	fmt.Println(string(body))
}

func main() {
	GetData()
	PostData()
	//var i int
	i:=1
	j:=3
	var k=i+j
	i,j =j,i
	fmt.Println("搞定！",j,k)
	//var nickName string
	_, _, nickName := GetName()
	fmt.Println("nickName名字是：",nickName)
	const c0  = iota
	const c1  = iota
	const c2  = iota
	fmt.Println("iota是：",c0,c1,c2)
	const (
		c3 = iota
		c4 = iota
		c5 = iota
	)
	fmt.Println("iota是：",c3,c4,c5)
	fmt.Println("浮点型比较：",math.Remainder(1.1, 1.2))
	fmt.Println("浮点型比较：",IsEqual(1.1,1.2,0.1))
	fmt.Println("数组切片：")
	MySlice()
	var mySlice2 = []int {1,2,3,4,5}
	fmt.Println("\nElements of mySlice2: ")
	for _, v := range mySlice2 {
		fmt.Print(v, " ")
	}
	mySlice3 := make([]int, 5, 10)
	fmt.Println("\nlen(mySlice):", len(mySlice3))
	fmt.Println("cap(mySlice):", cap(mySlice3))
	mySlice3 = append(mySlice3, 1, 2)
	fmt.Println("\nElements of mySlice3: ")
	for _, v := range mySlice3 {
		fmt.Print(v, " ")
	}
	mySlice4 := []int{8, 9, 10}
	// 给mySlice后面添加另一个数组切片
	mySlice3 = append(mySlice3, mySlice4...)
	fmt.Println("\nElements of mySlice3: ")
	for _, v := range mySlice3 {
		fmt.Print(v, " ")
	}
	oldSlice := []int{1, 2, 3, 4, 5}
	newSlice := oldSlice[:3] // 基于oldSlice的前3个元素构建新数组切片
	fmt.Println("\nlen(newSlice):", len(newSlice))
	fmt.Println("cap(newSlice):", cap(newSlice))
	fmt.Println("\nElements of newSlice: ")
	for _, v := range newSlice {
		fmt.Print(v, " ")
	}

	var personDB map[string] PersonInfo
	personDB = make(map[string] PersonInfo)
	// 往这个map里插入几条数据
	personDB["12345"] = PersonInfo{"12345", "Tom", "Room 203,..."}
	personDB["1"] = PersonInfo{"1", "Jack", "Room 101,..."}
	// 从这个map查找键为"1234"的信息
	person, ok := personDB["12345"]
	// ok是一个返回的bool型，返回true表示找到了对应的数据
	if ok {
		fmt.Println("\nFound person", person.Name, "with ID 1234.")
	} else {
		fmt.Println("\nDid not find person with ID 1234.")
	}
	fmt.Println("\n=====",example(1))
}
func GetName() (firstName, lastName, nickName string) {
	return "May", "Chan", "Chibi Maruko"
}
func IsEqual(f1, f2, p float64) bool {
	return math.Remainder(f1, f2) < p
}
func MySlice() {
	// 先定义一个数组
	var myArray [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	// 基于数组创建一个数组切片
	var mySlice []int = myArray[:5]
	var mySlice1 []int = myArray[5:]
	fmt.Println("Elements of myArray: ")
	for _, v := range myArray {
		fmt.Print(v, " ")
	}
	fmt.Println("\nElements of mySlice: ")
	for _, v := range mySlice {
		fmt.Print(v, " ")
	}
	fmt.Println("\nElements of mySlice1: ")
	for _, v := range mySlice1 {
		fmt.Print(v, " ")
	}
	fmt.Println()
}
// PersonInfo是一个包含个人详细信息的类型
type PersonInfo struct {
	ID string
	Name string
	Address string
}
func example(x int) int {
	if x == 0 {
		return 5
	} else {
		return x
	}
}